//
//  HomeViewController.swift
//  SwitfTest
//
//  Created by runone02 on 2018/12/28.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

import UIKit
import RxSwift
import SwiftTTPageController

class HomeViewController: BaseViewController ,TTPageViewControllerDelegate,TTHeadViewDelegate,UITextFieldDelegate{
    /// 控制器数组
    var vcArr = [RootAnimationTableViewController(),MainVC(),FriendRootController(),RulerViewController(),CreateQRCodeViewController(),HomerListViewController()]
    var pagevc : TTPageViewController!
    var topview : TTHeadView!
    
    ///设置状态栏的颜色
    override var preferredStatusBarStyle: UIStatusBarStyle {
        get{
            return .default
//            return .lightContent
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        automaticallyAdjustsScrollViewInsets = false;
        _init()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        if let has =  UserDefaults.standard.value(forKey: "isFirstLaunch") as? String , has == "1"{
            return
        }
    }
    

    
    func _init() {
        view.backgroundColor = UIColor.white
        ///标题名字
        let titles = ["最新","热门","问答","活动","求助","娱乐"]
        let _w :CGFloat = 50.0 * CGFloat(titles.count) < SCREEN_WIDTH ? 55 : 50
        var attri = TTHeadTextAttribute()
        attri.itemWidth = _w
        attri.defaultFontSize = 16
        attri.selectedFontSize = 17
        
        //头部滚动View
        topview = TTHeadView (frame: CGRect (x: 0, y: kTopBarHeight, width: SCREEN_WIDTH - 30, height: 35), titles: titles, delegate: self ,textAttributes:attri)
        topview.backgroundColor = UIColor.white
        
        self.navigationItem.titleView = topview
        
        let rec = CGRect (x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - 50)
        pagevc = TTPageViewController(controllers:vcArr, frame: rec, delegate:self)
        
        self.addChild(pagevc)
        view.addSubview(pagevc.view)
    }
    
    
    //MARK: -
    func tt_headViewSelectedAt(_ index: Int) {
        pagevc.scrollToPageAtIndex(index)
    }
    
    func tt_pageControllerSelectedAt(_ index: Int) {
        
        topview.scrollToItemAtIndex(index);
    }
    
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {

        return false
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
